
var loginPanelStatus = 0; // 0 for login, 1 for register


/*********************************************************************************************
 *
 * DOM functions.
 *
 **********************************************************************************************/
function showLoginLayer() {
  $(".login-layer").show();
};

function hideLoginLayer() {
  $(".login-layer").hide();
};

function showLoginPanel() {
  showLoginLayer();
  $(".register-panel").hide();
  $(".login-panel").show();
}

function showRegisterPanel() {
  showLoginLayer();
  $(".login-panel").hide();
  $(".register-panel").show();
}

function switchLoginPanel() {
  if($('.login-panel').css('display') == 'none') {
    showLoginPanel();
  } else {
    showRegisterPanel();
  }
}

function copyToClipboard(element) {
    var $temp = $("<input>")
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
}

function addServiceItem(serviceUser) {
  var serviceTable = $('.service-list tbody');
  var tr = document.createElement('tr');
      $(tr).attr('id', serviceUser.username + '-item');
      $(tr).html('<td>' + serviceUser.name + '</td>'
                 + '<td>********</td>'
                 + '<td><a href="/user/service/' + user.name + '">查看消息记录</a></td>'
                 + '<td><a href="javascript:;">修改密码</a></td>'
                 + '<td><a href="javascript:;">删除</a></td>');
      $(serviceTable).append(tr);
}

function showAddServiceContainer() {
  var container = $('.add-service-wrapper');
  $(container).children('input').val('');
  $(container).show();
}

function hideAddServiceContainer() {
  $('.add-service-wrapper').hide();
}

/*********************************************************************************************
 *
 * ajax functions.
 *
 **********************************************************************************************/

/**
 *
 * 获取并展示客服列表
 *
 */
function getServices() {
  var serviceTable = $('.service-list tbody');
  $(serviceTable).children().remove();
  $.get('/api/gets', function(data, status) {
    console.log('status: ' + status);
    console.log(data);
    for(d in data) {
      user = data[d];
      addServiceItem(user);
    }
  });
}

/**
 *
 * 删除一个客服
 *
 */
function requestDeleteService(item) {
  var serviceName = $(item).attr('id');
  serviceName = serviceName.substr(0, serviceName.length - 5);
  $.get('/api/deleteService', {serviceName: serviceName}, function(data, status) {
     getServices();
  });
}

/**
 *
 * 添加一个客服
 *
 */
function requestAddService(name, passwd) {
 hideAddServiceContainer();
 $.get('/api/addService', {serviceName: name, servicePasswd: passwd}, function(data, status) {
   getServices();
 });
}

$(document).ready(function() {
  $(".login-button").click(function() {
    showLoginPanel();
  });

  $(".login-panel-close").click(function() {
    hideLoginLayer();
  });

  $(".login-panel-header-trans").click(function() {
    switchLoginPanel();
  });

  $(".register-button").on('click', function() {
    showRegisterPanel();
  });

  $(".copy-button").click(function() {
    var iframeTextElement = $(this).parent().children("p")[0];
    copyToClipboard(iframeTextElement);
  });

  $('.add-service-submit').on('click', function() {
    var name = $('.add-service-name').val();
    var passwd = $('.add-service-passwd').val();
    requestAddService(name, passwd);
  });

  $('.add-service-button').on('click', function() {
    if($('.add-service-wrapper').css('display') == 'none') {
      showAddServiceContainer();
    }
  });
});